import unittest

import HTMLTestRunner

from TS_common.TS_common import shopcenter_add
from TS_common.TS_regirter import TinyShop_1
from TS_common.common import nameadd
from TS_common.tinyshop_login import TinyShop


class test_TinyShop(unittest.TestCase):
    def setUp(self):
        print("start...")
        self.add=nameadd()
        self.m=shopcenter_add()


    def test_fron_login1(self):         #输入正确的电子邮箱和密码，勾选自动登录            前台登录
        t = TinyShop()
        r = t.login_1("1446637478@qq.com","245248776","1")
        self.assertIn("欢迎你！",r)
    def test_fron_login2(self):         #输入正确的电子邮箱输入错误的密码，勾选自动登录
        t = TinyShop()
        r = t.login_1("1446637478@qq.com","gdh124","1")
        self.assertIn("error:true,msg:\"密码错误！\"",r)
    def test_fron_login3(self):         #输入没注册的电子邮箱和密码，勾选自动登录
        t = TinyShop()
        r = t.login_1("57575757@qq.com","gdh124","1")
        self.assertIn("error:true,msg:\"账号不存在！\"",r)
    def test_fron_login4(self):         #不输入电子邮箱和密码，勾选自动登录
        t = TinyShop()
        r = t.login_1("","","1")
        self.assertIn("error:true,msg:\"账号不存在！\"",r)
    def test_fron_login5(self):         #输入正确的电子邮箱和密码，不勾选自动登
        t = TinyShop()
        r = t.login_1("1446637478@qq.com","245248776","")
        self.assertIn("欢迎你！",r)
    def test_back_login1(self):         #输入正确的用户名密码和验证码             后台登录
        t = TinyShop()
        r = t.login_a("admin","123456","aaaa")
        self.assertIn("您好[admin]",r)
    def test_back_login2(self):         #输入正确的用户名错误的密码和正确的验证码
        t = TinyShop()
        r = t.login_a("admin","45245","aaaa")
        self.assertIn("用户名或者密码错误",r)
    def test_back_login3(self):         #输入错误的用户名错误密码和正确的验证码
        t = TinyShop()
        r = t.login_a("asdad","45246","aaaa")
        self.assertIn("用户名或者密码错误",r)
    def test_back_login4(self):         #不输入用户名密码和验证码
        t = TinyShop()
        r = t.login_a("","","aaaa")
        self.assertIn("用户名或者密码错误",r)
    def test_back_login5(self):         #输入正确的用户名密码和错误的验证码
        t = TinyShop()
        r = t.login_a("admin","123456","hjf4")
        self.assertIn("验证码错误",r)
    def test_back_login6(self):         #输入错误的用户名密码和错误的验证码
        t = TinyShop()
        r = t.login_a("sdff","53454","hjf4")
        self.assertIn("验证码错误",r)


    def test_case1(self):  #均填写正确
        t= TinyShop_1()
        r = t.regirter("test777777@qq.com","123456","123456")
        self.assertIn("value=\"http://mail." ,r)

    def test_case2(self):   #当邮箱为空时
        t= TinyShop_1()
        r = t.regirter("","123456","123456")
        self.assertIn("error:true,msg:\"邮箱不能为空！\"" ,r)

    def test_case3(self):#当密码和确认密码为空时
        t= TinyShop_1()
        r = t.regirter("test8888885@qq.com","","")
        self.assertIn("error:true,msg:\"密码长度必需大于6位！\"" ,r)

    def test_case4(self): #当密码和确认密码小于六位数时
        t= TinyShop_1()
        r = t.regirter("test8888886@qq.com","123","123")
        self.assertIn("error:true,msg:\"密码长度必需大于6位！\"" ,r)

    def test_case5(self): #当两次密码不一致时
        t= TinyShop_1()
        r = t.regirter("test8888887@qq.com","123456","654321")
        self.assertIn("error:true,msg:\"两次密码输入不一致！\"" ,r)

    def test_case6(self): #当邮箱，密码，确认密码都不输入时
        t= TinyShop_1()
        r = t.regirter("","","")
        self.assertIn("error:true,msg:\"邮箱不能为空！\"" ,r)

    def test_case7(self): #当验证码输入错误时
        t= TinyShop_1()
        r = t.regirter("test999999@qq.com","123456","654321")
        self.assertIn("error:true,msg:\"验证码错误!\"" ,r)
    def test_case8(self):  #当输入已注册的邮箱时
        t= TinyShop_1()
        r = t.regirter("test999999@qq.com","123456","123456")
        self.assertIn("error:true,msg:\"此用户已经被注册！\"" ,r)

    def test_case_add1(self):
        print("test_case_add1")
        # 所有参数正确
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'2','sell_price':'100',
                     'market_price':'120','cost_price':'30','weight':'500'})
        self.assertIn('上架',re)
    def test_case_add2(self):
        print("test_case_add2")
        #缺少分类
        re=self.add.add({'category_id':'','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'2','sell_price':'100',
                     'market_price':'120','cost_price':'30','weight':'500'})
        self.assertIn("选择分类，若无分类请先创建",re)

    def test_case_add3(self):
        print("test_case_add3")
        #缺少商品名称
        re=self.add.add({'category_id':'5','name':'','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'2','sell_price':'100',
                     'market_price':'120','cost_price':'30','weight':'500'})
        self.assertIn("不能为空",re)

    def test_case_add4(self):
        print("test_case_add4")
        #缺少商品编号
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'2','sell_price':'100',
                     'market_price':'120','cost_price':'30','weight':'500'})
        self.assertIn("请输入3个以上的字符(不能为中文)",re)

    def test_case_add5(self):
        print("testcase5")
        #缺少图片
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'',
                     'img':'',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'2','sell_price':'100',
                     'market_price':'120','cost_price':'30','weight':'500'})
        self.assertIn("添加商品图片",re)

    def test_case_add6(self):
        print("test_case_add6")
        #缺少货号
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'','store_nums':'10','warning_line':'2','sell_price':'100',
                     'market_price':'120','cost_price':'30','weight':'500'})
        self.assertIn("请输入3个以上的字符(不能为中文)",re)

    def test_case_add7(self):
        print("test_case_add7")
        #缺少库存
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'','warning_line':'2','sell_price':'100',
                     'market_price':'120','cost_price':'30','weight':'500'})
        self.assertIn("必需为整数",re)

    def test_case_add8(self):
        print("test_case_add8")
        #缺少预警线
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'','sell_price':'100',
                     'market_price':'120','cost_price':'30','weight':'500'})
        self.assertIn("必需为整数",re)

    def test_case_add9(self):
        print("test_case_add9")
        #缺少重量
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'2','sell_price':'100',
                     'market_price':'120','cost_price':'30','weight':''})
        self.assertIn("必需为整数",re)

    def test_case_add10(self):
        print("test_case_add10")
        #缺少零售价
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'2','sell_price':'',
                     'market_price':'120','cost_price':'30','weight':'500'})
        self.assertIn("整数或保留小数点后两位精确度的数",re)

    def test_case_add11(self):
        print("test_case_add11")
        #缺少市场价
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'2','sell_price':'100',
                     'market_price':'','cost_price':'30','weight':'500'})
        self.assertIn("整数或保留小数点后两位精确度的数",re)

    def test_case_add12(self):
        print("test_case_add12")
        #缺少市场价
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'2','sell_price':'100',
                     'market_price':'120','cost_price':'','weight':'500'})
        self.assertIn("整数或保留小数点后两位精确度的数",re)

    def test_case_add13(self):
        print("test_case_add13")
        #所有参数为空
        re=self.add.add({'category_id':'','name':'','goods_no':'',
                     'point':'','sort':'','unit':'',
                     'imgs':'',
                     'img':'',
                     'pro_no':'','store_nums':'','warning_line':'','sell_price':'',
                     'market_price':'','cost_price':'','weight':''})
        self.assertIn("整数或保留小数点后两位精确度的数",re)


    def test_all_correct(self):##传入全部参数
        print("this is testcase1")
        re=self.m.shop_add({'name':'admin','password':'123456','repassword':'123456','email':'133478240@qq.com','real_name':'x',
                            'sex':'0','birthday':'1993-12-21','province':'350000','city':'350100','county':'350103','addr':'','phone':'13423231111','mobile':'7878564','point':'0'})
        self.assertIn("充值",re)
    def test_must(self):##值传入必须参数
         print("this is testcase2")
         re=self.m.shop_add({'name':'admi','password':'123456','repassword':'123456','email':'133478@40qq.com','real_name':'x',
                          'province':'350000','city':'350100','county':'350103','point':'0'})
         self.assertIn("充值",re)
    def test_exit(self):
         print("this is testcase3")##用户名重复
         re=self.m.shop_add({'name':'admin','password':'123456','repassword':'123456','email':'133478%@40qq.com','real_name':'x',
                          'province':'350000','city':'350100','county':'350103','point':'0'})
         self.assertNotIn("充值",re)
    def test_null(self):
         print("this is testcase4")##用户名为空
         re=self.m.shop_add({'name':'','password':'123456','repassword':'123456','email':'133478%@140qq.com','real_name':'x',
                          'province':'350000','city':'350100','county':'350103','point':'0'})
         self.assertNotIn("充值",re)
    def test_teshu(self):
         print("this is testcase5")##用户名为特殊字符
         re=self.m.shop_add({'name':'￥%……','password':'123456','repassword':'123456','email':'133478%@140qq.com','real_name':'x',
                          'province':'350000','city':'350100','county':'350103','point':'0'})
         self.assertIn("充值",re)
    def test_long(self):
         print("this is testcase6")##用户名为超长字符
         re=self.m.shop_add({'name':'123456767888erererer','password':'123456','repassword':'123456','email':'133478%@40qq.com','real_name':'x',
                          'province':'350000','city':'350100','county':'350103','point':'0'})
         self.assertIn("充值",re)
    def test_password_null(self):
         print("this is testcase7")##密码为空
         re=self.m.shop_add({'name':'x','password':'','repassword':'123456','email':'133478@40qq.com','real_name':'x',
                          'province':'350000','city':'350100','county':'350103','point':'0'})
         self.assertNotIn("充值",re)
    def test_password_small(self):
         print("this is testcase8")##密码小于六位
         re=self.m.shop_add({'name':'x','password':'123','repassword':'123','email':'133478@40qq.com','real_name':'x',
                          'province':'350000','city':'350100','county':'350103','point':'0'})
         self.assertNotIn("充值",re)



    def tearDown(self):
        print("end...")
if __name__ =="__main__":
    # unittest.main()
    su = unittest.TestSuite(unittest.TestLoader().loadTestsFromTestCase(test_TinyShop))
    f = open("../TS_TestRunner/TinyShop.html","wb")
    r = HTMLTestRunner.HTMLTestRunner(
        stream=f,
        title="TinyShop-测试报告",
        description="通过情况"
    )
    r.run(su)
    f.close()